package com.wings4.security

import java.text.SimpleDateFormat
import com.wings4.security.utils.EncryptionUtils
import com.wings4.coresecurity.AuthenticationToken

class AuthController {
    def securityService;

    def index= {
    }

//    def getPassword(){
//        println(new EncryptionUtils("wings4").decrypt("cp/Shjc3Ikc="));
//    }

    def login={
        def targetUri = params.targetUri;
        println("auth");
        try{
            if (params.targetUri != null){
                response.setHeader(params.targetUri, "")
            }
        }  catch (Exception ex){
             ex.stackTrace;
        }
        render(view: "login",  model: [targetUri: targetUri]);
    }

    def authenticateUser={
        AuthenticationToken authToken = securityService.doAuthentication(params);
        def user = authToken.principal;
//        println(authenticateUser);

        if (user?.active){
            session.user = user;
            if(!session.user){
                redirect(action: "login")
                return false;
            } else {
                session.credentials = authToken.credential;
                session.grantedAuthority = authToken.grantedAuthority;

                session.modules = securityService.loadControllers();
                log.debug("grantedAuthority >>> " + session.grantedAuthority);
                if (session.requestedController != null && session.requestedAction != null){
                    redirect(controller:session.requestedController, action:session.requestedAction);
                }else{
                    redirect(controller:"application", action: "index");
                }
//
//                Employee employee = Employee.findByUserCode(session.user.getUserCode());
//                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//                Calendar now = Calendar.getInstance();
//                String date = Calendar.getInstance().getTime().getDate().toString();
//                String month = (now.get(Calendar.MONTH) + 1).toString();
//                String year = Calendar.getInstance().get(Calendar.YEAR).toString();
//                String dateFormat = year+"-"+month+"-"+date;
//                java.util.Date logInDate = formatter.parse(dateFormat);
//                if (User.isInstance(employee)){
//                    LogInInformation logInInformation1 = LogInInformation.findByEmployeeAndLoginDateAndLogOutDateIsNull(employee,logInDate);
//                    logInInformation1.setLogOutDate(Calendar.getInstance().getTime());
//                    logInInformation1.setLogOutTime(Calendar.getInstance().getTime());
//                    logInInformation1.merge();
//                }
//                else {
//                    println("Administrative User");
//                }
//
//                LogInInformation logInInformation = new LogInInformation();
//                logInInformation.loginDate = Calendar.getInstance().getTime();
//                logInInformation.loginTime = Calendar.getInstance().getTime();
//
//                logInInformation.employee = employee;
//                logInInformation.save(flush: true)

                return true;
            }

        } else {
            flash.message = "Bad Credentials!!!";
            redirect(action: "login");
        }
    }
}
